<template>
    <div class="menu-bar">
        <h1 class="logo" v-show="log"> <span>音乐网站</span> </h1>
        <ul ref="headernev">
            <li>
                <a href="/layout/fronthome">音乐</a>
                <div class="dropmenu">
                    <ul>
                        <li><a href="/layout/fronthome">首页</a></li>
                        <li><a href="/layout/frontsinger">歌手</a></li>
                        <li><a href="/layout/frontsonglist">歌单</a></li>
                        <li><a href="#">MV</a></li>
                    </ul>
                </div>
            </li>
            <li><a href="/layout/platefrom">社区</a></li>
            <li><a href="/layout/personal">个人中心</a></li>
            <li><a href="/layout/about">关于我们</a></li>
            <li><a href="/home">管理系统</a></li>
        </ul>
        <div class="search-input">
            <el-input v-model="name" placeholder="输入内容" suffix-icon="el-icon-search" style="width: 200px;margin-left:  30px;" @keyup.enter="goSearch()"></el-input>
            <el-button type="primary" style="margin: 0 30px 0 5px; width: 80px;height: 40px;" @click="goSearch()">搜索</el-button>
        </div>
        <div class="lecont" v-show="avator">
            <a href="/logre" v-if="this.token == ''">登录/注册</a>
            <div v-else style="margin-left:15px;">
                <el-dropdown @command="handleCommand">
                    <span class="el-dropdown-link">
                        <img :src="this.user.image" class="imguser">
                    </span>
                    <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center;">
                        <el-dropdown-item command="pre"><a>个人中心</a></el-dropdown-item>
                        <el-dropdown-item command="out"><a>退出</a></el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>

        <div class="brad" v-show="bradhead">
            <span class="iconfont icon-gengduo"></span>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
import { mapMutations, mapState} from 'vuex'
export default {
    data(){
        return{
            name:'',
            isShow:false,
            user:JSON.parse(localStorage.getItem('honey-user') || '{}'),
            token:JSON.parse(localStorage.getItem('token') || '{}'),
            screenWidth: document.body.clientWidth,
            bradhead:false,
            avator:true,
            log:true
        }
    },
    methods:{
        ...mapMutations('search',['getKeyword']),
        goSearch(){
            this.$router.push("/layout/searchlist")
            this.getKeyword(this.name)
            window.location.reload()
        },
        handleCommand(command){
            if(command == "out"){
                axios({
                url:this.$baseUrl + '/User/logout',
                method:"get",
                headers:{
                    token:JSON.parse(localStorage.getItem('token') || '{}')
                }
            }).then((response)=>{
                console.log(response)
            })
                this.token = ''
                localStorage.removeItem('token')
                localStorage.removeItem('user')
            }
            if(command == "pre"){
                this.$router.push("/layout/personal")
            }
            console.log("------------------------")
        }
    },


    watch: {
        screenWidth: {
            immediate: true,
            handler: function(newVal) {
                console.log("窗口变化")
                if (!this.timer) {
                    this.screenWidth = newVal;
                    this.timer = true;
                    if(newVal<=768){
                        this.log = false
                    }
                    else{
                        this.log = true
                    }
                    if(newVal<=500){
                        this.avator = false
                    }
                    else{
                        this.avator = true
                    }
                    let fontsize=newVal/100
                    console.log(fontsize)
                    let _this = this;
                    setTimeout(() => {
                        _this.timer = false;
                    }, 400);
                }
            }
        }
    },

    mounted() {
        window.addEventListener("resize", () => {
            this.screenWidth = document.body.clientWidth;
            this.screenHeight = document.body.clientHeight;
        });  
    },
    computed:{

    },
}
</script>

<style lang="less" scoped>
*{
    margin: 0 ;
    padding: 0;
    box-sizing: border-box;
}

body{
    font-family: sans-serif;
}

.logo span{
    color: #FFD700;
    font-size: 30px;

}

.menu-bar{
    height: 80px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 5%;
    position: relative;
    background-color: rgba(0, 0, 0,0.4);
}

.menu-bar ul{
    list-style: none;
    display: flex;
}

.menu-bar ul li{
    text-decoration: none;
    padding: 10px 40px;
    position: relative;
}

.menu-bar ul li a{
    font-size: 20px;
    color: azure;
    text-decoration: none;
    transition: all 0.3s;

}

.menu-bar ul li :hover{
    color:greenyellow
}

.menu-bar .dropmenu{
    display: none;
}

.menu-bar ul li:hover .dropmenu{
    display: block;
    position: absolute;
    left: 0;
    top:35px;
    background-color: black; 
    opacity: 0.5;
}

.menu-bar ul li:hover .dropmenu ul{
    display: flex;
    margin: 10px;
}
.dropmenu:hover .dropmenu ul{
    display:block;
}
.menu-bar ul li:hover .dropmenu ul li{
    width: 120px;
    text-align: center;
}
.search-input{
    margin-top: 4px;
}

.imguser{
  width: 70px;
  height: 70px;
  border-radius: 50%;
}
.el-dropdown-link{
  margin-right: 40px;
}
.lecont a{
    font-size: 20px;
    color: azure;
    text-decoration: none;
    transition: all 0.3s;
}

.lecont a:hover{
    color:greenyellow
}

@media (max-width:1640px) {
    .menu-bar ul li{
        text-decoration: none;
        padding: 5px 20px;
        position: relative;
    }
}


@media (max-width:1200px) {
    .menu-bar ul li{
        text-decoration: none;
        padding: 5px;
        position: relative;
    }
}

@media (max-width:1024px){
    .menu-bar ul li a{
        font-size: 15px;
        color: azure;
        text-decoration: none;
        transition: all 0.3s;
    }
    // .menu-bar ul{
    //     list-style: none;
    //     display: block;
    // }
}
@media (max-width:768px){

    .menu-bar ul{
        margin-top:100px;
        display: block;
    }

    .menu-bar ul li:hover .dropmenu{
        display: block;
        position: absolute;
        left: -100%;
        // top:35px;
        background-color: black; 
        opacity: 0.5;
    }
    .menu-bar ul li:hover .dropmenu ul{
        display: block;
        margin: 10px;
    }
    .menu-bar ul li:hover .dropmenu ul li{
        width: 100%;
        text-align: center;
    }
}

@media (max-width:500px){
    .search-input{
        display: flex;
    }
}
</style>